package
{
	import com.bit101.components.Component;
	import com.bit101.components.HBox;
	import com.bit101.components.HSlider;
	import com.bit101.components.HUISlider;
	import com.bit101.components.Label;
	import com.bit101.components.Panel;
	import com.bit101.components.Slider;
	import com.bit101.components.UISlider;
	import com.bit101.components.VBox;
	import com.bit101.components.VUISlider;
	import com.bit101.components.Window;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.utils.Dictionary;
	
	import flashx.textLayout.property.Property;

	public class ControlBoard extends Sprite
	{
		protected var activationCode : String;
		protected var activationIndex : int;
		protected var componentToTarget : Dictionary = new Dictionary();
		protected var targetToComponent : Dictionary = new Dictionary();
		protected var properties : Array = [];
		
		
			public var window0 : com.bit101.components.Window ;
			public var vbox1 : com.bit101.components.VBox ;
			public var huislider2 : com.bit101.components.HUISlider ;
			public var huislider3 : com.bit101.components.HUISlider ;

				
		public function ControlBoard( parent : DisplayObjectContainer )
		{
			init();
			addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage );
			parent.addChild(this);
			
		}
		
		public function init():void
		{
			
			
			window0 = new com.bit101.components.Window( this );
			window0.setSize(240,80);
			window0.minimized = false;
			window0.hasMinimizeButton = true;
			window0.hasCloseButton = true;
			window0.addEventListener(Event.CLOSE,handleClose);
			vbox1 = new com.bit101.components.VBox( window0 );
			huislider2 = new com.bit101.components.HUISlider( vbox1 );
			huislider2.label="speed";
			huislider2.minimum = -1;
			huislider2.maximum = 50;
			huislider3 = new com.bit101.components.HUISlider( vbox1 );
			huislider3.label="gravity";
			huislider3.minimum = -10;
			huislider3.maximum = 10;

		}
		
		public function attachControlToProperty( targetObject : Object, key : String, component : Component, watch : Boolean = false ):void
		{
			var property : Property = componentToTarget[component] = new Property(key, targetObject );
			
			if( targetObject in targetToComponent ) ( targetToComponent[targetObject] as Array ).push( component );
			else targetToComponent[targetObject] = [ component ];
			
			setValueOnComponent( property, component );
			
			if( watch && targetObject is IEventDispatcher )
			{
				IEventDispatcher( targetObject ).addEventListener( Event.CHANGE, handleChangeInTarget );
			}
			
			if(component is UISlider)
			{
				component.addEventListener(Event.CHANGE,handleChangeInComponent);
			}
		}
				
		protected function setValueOnTarget( property : Property, component : Component ):void
		{
			if( component is Slider )
			{
				property.targetObject[property.key] = Slider( component ).value; 				
			}
			else if( component is HUISlider )
			{
				property.targetObject[property.key] = HUISlider( component ).value; 			
			}
			else if( component is VUISlider )
			{
				property.targetObject[property.key] = VUISlider( component ).value; 			
			}
		}
		
		protected function setValueOnComponent( property : Property, component : Component ):void
		{
			if( component is Slider )
			{
				Slider( component ).value = property.targetObject[property.key];	
			}
			else if( component is HUISlider )
			{
				HUISlider( component ).value = property.targetObject[property.key];	
			}
			else if( component is VUISlider )
			{
				VUISlider( component ).value = property.targetObject[property.key];	
			}
			
		}
		
		protected function handleChangeInComponent( e : Event ):void
		{
			var target : Component = e.target as Component;
			var property : Property = componentToTarget[target];
			
			if( ! property ) return;
			
			setValueOnTarget( property, target );
		}

		protected function handleChangeInTarget( e : Event ):void
		{
			var target : IEventDispatcher = e.target as IEventDispatcher;
			var components : Array = targetToComponent[target];
			for each( var component : Component in components )
			{
				var property : Property = componentToTarget[component];
				setValueOnComponent( property, component );
			}
			
		}
		
		protected function handleAddedToStage(e:Event):void
		{
			if( activationCode )
			{
				visible = false;
				stage.addEventListener( KeyboardEvent.KEY_UP, handleKeyUp );
			}
		}
		
		protected function handleClose(e:Event):void
		{
			visible = false;
		}
		
		protected function handleKeyUp(e:KeyboardEvent):void
		{
			var key : String = String.fromCharCode( e.charCode );
			if( key == activationCode.charAt( activationIndex) )
			{
				activationIndex++;
				if( activationIndex == activationCode.length )
				{
					visible = true;
					activationIndex = 0;
				}
			}else
			{
				activationIndex = 0;
			}
		}

	}
	

}

internal class Property
{
	public var key : String;
	public var targetObject : Object;
	
	public function Property(key:String, object:Object)
	{
		this.key = key;
		targetObject = object;
	}
}
